<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
        <meta name="apple-mobile-web-app-capable" content="yes" />

        <!-- Base Css Files -->
        <link href="__PUBLIC__/statics/coco-chat/assets/libs/jqueryui/ui-lightness/jquery-ui-1.10.4.custom.min.css" rel="stylesheet" />
        <link href="__PUBLIC__/statics/coco-chat/assets/libs/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
        <link href="__PUBLIC__/statics/coco-chat/assets/libs/font-awesome/css/font-awesome.min.css" rel="stylesheet" />
        <link href="__PUBLIC__/statics/coco-chat/assets/libs/fontello/css/fontello.css" rel="stylesheet" />
        <link href="__PUBLIC__/statics/coco-chat/assets/libs/animate-css/animate.min.css" rel="stylesheet" />
        <link href="__PUBLIC__/statics/coco-chat/assets/libs/nifty-modal/css/component.css" rel="stylesheet" />
        <link href="__PUBLIC__/statics/coco-chat/assets/libs/magnific-popup/magnific-popup.css" rel="stylesheet" /> 
        <link href="__PUBLIC__/statics/coco-chat/assets/css/style.css" rel="stylesheet" type="text/css" />
        <link href="__PUBLIC__/statics/coco-chat/assets/css/style-responsive.css" rel="stylesheet" />
        <link href="__PUBLIC__/statics/coco-chat/assets/libs/pace/pace.css" rel="stylesheet" />
        <link href="__PUBLIC__/statics/coco-chat/assets/libs/jquery-notifyjs/styles/metro/notify-metro.css" rel="stylesheet" type="text/css" />
        
        <!-- Extra CSS Files -->
      	<link href="__PUBLIC__/statics/coco-chat/assets/libs/bootstrap-validator/css/bootstrapValidator.min.css" rel="stylesheet"/>
      	<link href="__PUBLIC__/statics/coco-chat/assets/libs/jquery-icheck/skins/all.css" rel="stylesheet" />
      	
        <!-- Custom Css Files -->
        <link href="__ADMIN_CSS__/style.css"  rel="stylesheet" />
    </head>
    <body>
	<!-- Begin page -->
	<div>
		<div class="sub-header-title">
			<h2>修改管理员</h2>
		</div>
		<form role="form" id="edit-form" class="form-horizontal" action="{:U('Admin/edit')}">
			<input type="hidden" name="id" value="{$admin.id}">
			<div class="form-group">
				<label class="col-sm-2 control-label"><span class="red_star">*</span>管理组</label>
				<div class="col-sm-5">
					<input type="hidden" name="auth_group">
					<volist name="groups" id="vo">
						<label class="checkbox-inline icheckbox"><input type="checkbox" class="auth-group" value="{$vo.id}" <if condition="in_array($vo['id'],$admin_groups)">checked</if>>{$vo.title}</label>
                 	</volist>
				</div>
				<div class="col-sm-5">
					<p class="help-block">至少选择一个角色(可以多选)</p>
				</div>
			</div>
		  	<div class="form-group">
				<label for="username" class="col-sm-2 control-label">用户名</label>
				<div class="col-sm-5">
				  	<input type="text" class="form-control" id="username" name="username" readonly value="{$admin.username}">
				</div>
				<div class="col-sm-5">
					<p class="help-block">管理员用户名不能更改</p>
				</div>
			</div>
			<div class="form-group">
				<label for="password" class="col-sm-2 control-label">密码</label>
				<div class="col-sm-5">
				  	<input type="password" class="form-control" id="password" name="password" value="">
				</div>
				<div class="col-sm-5">
					<p class="help-block">6~20个字符之间(必须与确认密码一致)，为空表示不修改密码</p>
				</div>
			</div>
			<div class="form-group">
				<label for="re_password" class="col-sm-2 control-label">确认密码</label>
				<div class="col-sm-5">
				  	<input type="password" class="form-control" id="re_password" name="re_password" value="">
				</div>
				<div class="col-sm-5">
					<p class="help-block">6~20个字符之间(必须与新密码一致)，为空表示不修改密码</p>
				</div>
			</div>
			<div class="form-group">
				<label for="realname" class="col-sm-2 control-label"><span class="red_star">*</span>真实姓名</label>
				<div class="col-sm-5">
				  	<input type="text" class="form-control" id="realname" name="realname" value="{$admin.realname}">
				</div>
				<div class="col-sm-5">
					<p class="help-block">必须在2~20个字符之间</p>
				</div>
			</div>
			<div class="form-group">
				<label for="nickname" class="col-sm-2 control-label">昵称</label>
				<div class="col-sm-5">
				  	<input type="text" class="form-control" id="nickname" name="nickname" value="{$admin.nickname}">
				</div>
				<div class="col-sm-5">
					<p class="help-block">2~20个字符之间</p>
				</div>
			</div>
			<div class="form-group">
				<label for="phone" class="col-sm-2 control-label">手机号码</label>
				<div class="col-sm-5">
				  	<input type="text" class="form-control" id="phone" name="phone" value="{$admin.phone}">
				</div>
				<div class="col-sm-5">
					<p class="help-block">4~20个数字之间(必须都为数字)</p>
				</div>
			</div>
			<div class="form-group">
				<label for="email" class="col-sm-2 control-label">email</label>
				<div class="col-sm-5">
				  	<input type="text" class="form-control" id="email" name="email" value="{$admin.email}">
				</div>
				<div class="col-sm-5">
					<p class="help-block">6~50个字符之间(必须符合email的格式)</p>
				</div>
			</div>
			<div class="form-group">
				<div class="col-sm-offset-4 col-sm-4">
                    <button type="submit" class="btn btn-success">保存</button>
                </div>
			</div>
		</form>
	</div>
	<!-- End of page -->
	
	<!-- Base Js Files -->
	<script src="__PUBLIC__/statics/coco-chat/assets/libs/jquery/jquery-1.11.1.min.js"></script>
	<script src="__PUBLIC__/statics/coco-chat/assets/libs/bootstrap/js/bootstrap.min.js"></script>
	<script src="__PUBLIC__/statics/coco-chat/assets/libs/jqueryui/jquery-ui-1.10.4.custom.min.js"></script>
	<script src="__PUBLIC__/statics/coco-chat/assets/libs/jquery-ui-touch/jquery.ui.touch-punch.min.js"></script>
 	<script src="__PUBLIC__/statics/coco-chat/assets/libs/jquery-detectmobile/detect.js"></script>
	<script src="__PUBLIC__/statics/coco-chat/assets/libs/fastclick/fastclick.js"></script>
	<script src="__PUBLIC__/statics/coco-chat/assets/libs/jquery-slimscroll/jquery.slimscroll.js"></script>
	<script src="__PUBLIC__/statics/coco-chat/assets/libs/nifty-modal/js/classie.js"></script>
	<script src="__PUBLIC__/statics/coco-chat/assets/libs/nifty-modal/js/modalEffects.js"></script>
	<script src="__PUBLIC__/statics/coco-chat/assets/libs/jquery-notifyjs/notify.min.js"></script>
    <script src="__PUBLIC__/statics/coco-chat/assets/libs/jquery-notifyjs/styles/metro/notify-metro-blue.js"></script>
	<script src="__PUBLIC__/statics/coco-chat/assets/libs/pace/pace.min.js"></script>
	
	<!-- Extra Js Files -->
	<script src="__PUBLIC__/statics/coco-chat/assets/libs/bootstrap-validator/js/bootstrapValidator.min.js"></script>
	<script src="__PUBLIC__/statics/coco-chat/assets/libs/jquery-icheck/icheck.min.js"></script>
	
	<!-- Custom Js Files -->
	<script src="__PUBLIC_JS__/sha1.js"></script>
	<script src="__ADMIN_JS__/common.js"></script>
	
	<script>
	$(function(){
		//ICHECK
		$('input').iCheck({
		  	checkboxClass: 'icheckbox_square-blue',
		  	radioClass: 'iradio_square-blue',
		  	increaseArea: '20%' // optional
		});
		$("input").on('ifChecked', function(event){ 
			$('#edit-form').bootstrapValidator('disableSubmitButtons', false);
		}); 
		$("input").on('ifUnchecked', function(event){ 
			if($(".auth-group:checked").length==0){
				$('#edit-form').bootstrapValidator('disableSubmitButtons', true);
			}
		});
		
		$('#edit-form').bootstrapValidator({
	        message: '',
	        trigger: 'blur',
	        submitHandler: function(validator, form, submitButton) {
		      	//询问框
				window.parent.layer.confirm('确认添加管理员吗？', {
					btn: ['确认','取消'], //按钮
				    cancel: function(index){ 
				    	window.parent.layer.close(index);
						$('#edit-form').bootstrapValidator('disableSubmitButtons', false);
					}
				}, function(index){
					
					//封装管理组
					var groups = new Array();
					$(".auth-group:checked").each(function(){
						groups.push($(this).val());
					});
					$("input[name='auth_group']").val(JSON.stringify(groups));
					
					//开启数据处理提示
					var load = window.parent.layer.msg('数据保存中。。。', {
								  icon: 16,
								  shade: 0.5,
								  time: 0,
								});
					var f_data = $('form').serializeArray();
					var data = {};
					$.each(f_data, function(i, field){
						if(field.value != '' && (field.name=='password' || field.name=='re_password')) {
							data[field.name] = hex_sha1(field.value);
						} else {
							data[field.name] = field.value;
						}
				  	});
					// 用ajax提交表单
			      	$.post(form.attr('action'), data, function(response) {
			      		//关闭数据处理提示
						window.parent.layer.close(load);
			      		//弹出提示
						window.parent.layer.msg(response.message);
			      		
			      		//根据请求相应状态提示通知
			       		if(response.status){
			       			notify("success",response.message);
							window.location.href = "{:U('Admin/index')}";
						}else{
							notify("error",response.message);
						}
			        }, 'json');
			      	window.parent.layer.close(index);
				}, function(index){
					window.parent.layer.close(index);
					$('#edit-form').bootstrapValidator('disableSubmitButtons', false);
				});
	        },
	        fields: {
	            password: {
	                validators: {
	                	stringLength: {
	                        min: 6,
	                        max: 20,
	                    },
	                    callback: {
	    					callback:function(value, validator){
	    						var re_password = $('#edit-form').find("input[name=re_password]").val();
	    						if (value != re_password) {
	    							$('#edit-form').data('bootstrapValidator').updateElementStatus($('#edit-form').find("input[name='re_password']"), "INVALID", null );
	    						} else {
	    							$('#edit-form').data('bootstrapValidator').updateElementStatus($('#edit-form').find("input[name='re_password']"), "VALID", null );
	    						}
	    						return true;
	    					}
	    				}
	                }
	            },
	            re_password: {
	                validators: {
	                	stringLength: {
	                        min: 6,
	                        max: 20,
	                    },
	                    identical: {
	                        field: 'password',
	                    }
	                }
	            },
	            realname: {
	                validators: {
	                	notEmpty: {
	                    },
	                    stringLength: {
	                        min: 2,
	                        max: 20,
	                    },
	                }
	            },
	            nickname: {
	                validators: {
	                    stringLength: {
	                        min: 2,
	                        max: 20,
	                    },
	                }
	            },
	            phone: {
	                validators: {
	                	stringLength: {
	                        min: 4,
	                        max: 20,
	                    },
	                    digits: {
	                    }
	                }
	            },
	            email: {
	                validators: {
	                	stringLength: {
	                        min: 6,
	                        max: 20,
	                    },
	                    emailAddress: {
	                    }
	                }
	            }
	        }
	    });
	});
	</script>
	</body>
</html>